﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class Account : BasePage
{
    CustomerService _customerService;
    public Account()
    {
        _customerService = new CustomerService();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

        if (!string.IsNullOrEmpty(Request.QueryString["c"]))
        {
            if (!IsPostBack)
            {

                Customer customer= _customerService.GetAddressInfoByAccountId(Convert.ToInt32(Request.QueryString["c"]));
                if (customer != null)
                {
                    txtAccountName.Text = customer.Code;

                    TextBox txtFirstName = (TextBox)MainContactSubmit1.FindControl("txtFirstName");
                    txtFirstName.Text = customer.MainContact.FirstName;
                    TextBox txtLastName = (TextBox)MainContactSubmit1.FindControl("txtLastName");
                    txtLastName.Text = customer.MainContact.LastName;

                    TextBox txtEmail = (TextBox)MainContactSubmit1.FindControl("txtEmail");
                    txtEmail.Text = customer.MainContact.Email;
                    TextBox txtPhone = (TextBox)MainContactSubmit1.FindControl("txtPhone");
                    txtPhone.Text = customer.MainContact.Phone;
                    TextBox txtCompany = (TextBox)MainContactSubmit1.FindControl("txtCompany");
                    txtCompany.Text = customer.CompanyName;

                    TextBox txtStreet1 = (TextBox)MainContactSubmit1.FindControl("txtStreet1");
                    txtStreet1.Text = customer.MainContact.Location.Address;
                    TextBox txtStreet2 = (TextBox)MainContactSubmit1.FindControl("txtStreet2");
                    txtStreet2.Text = customer.MainContact.Location.Street;
                    TextBox txtCity = (TextBox)MainContactSubmit1.FindControl("txtCity");
                    txtCity.Text = customer.MainContact.Location.City;
                    DropDownList ddlState = (DropDownList)MainContactSubmit1.FindControl("ddlState");
                    ddlState.SelectedValue = customer.MainContact.Location.State.Code;
                    TextBox txtZipCode = (TextBox)MainContactSubmit1.FindControl("txtZipCode");
                    txtZipCode.Text = customer.MainContact.Location.ZipCode;
                    DropDownList ddlCountry = (DropDownList)MainContactSubmit1.FindControl("ddlCountry");
                    ddlCountry.SelectedValue = customer.MainContact.Location.Country.Code;
                }
            }
            btnSave.Text = "Update";
            lblAddAccount.Text = "UPDATE ACCOUNT : " + txtAccountName.Text;
            divAccountName.Visible = false;
        }
        else
        {
            btnSave.Text = "Add";
            lblAddAccount.Text = "ADD NEW ACCOUNT";
            divAccountName.Visible = true;
        }

        

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        TextBox txtFirstName=(TextBox) MainContactSubmit1.FindControl("txtFirstName");
        TextBox txtLastName=(TextBox) MainContactSubmit1.FindControl("txtLastName");


        TextBox txtEmail=(TextBox) MainContactSubmit1.FindControl("txtEmail");
        TextBox txtPhone=(TextBox) MainContactSubmit1.FindControl("txtPhone");
        TextBox txtCompany=(TextBox) MainContactSubmit1.FindControl("txtCompany");
        TextBox txtStreet1=(TextBox) MainContactSubmit1.FindControl("txtStreet1");
        TextBox txtStreet2=(TextBox) MainContactSubmit1.FindControl("txtStreet2");
        TextBox txtCity=(TextBox) MainContactSubmit1.FindControl("txtCity");
        DropDownList ddlState=(DropDownList) MainContactSubmit1.FindControl("ddlState");
        TextBox txtZipCode=(TextBox) MainContactSubmit1.FindControl("txtZipCode");
        DropDownList ddlCountry=(DropDownList) MainContactSubmit1.FindControl("ddlCountry");

        if (!string.IsNullOrEmpty(Request.QueryString["c"]))
        {
            _customerService.AddAccountAdminAndUserDefaulf(Convert.ToInt32(Request.QueryString["c"]), txtAccountName.Text, txtFirstName.Text, txtLastName.Text,txtCompany.Text,txtEmail.Text,txtPhone.Text,txtStreet1.Text,txtStreet2.Text,"",txtCity.Text,ddlState.SelectedValue,ddlCountry.SelectedValue,txtZipCode.Text);

        }
        else
        {
            _customerService.AddAccountAdminAndUserDefaulf(0, txtAccountName.Text, txtFirstName.Text, txtLastName.Text, txtCompany.Text, txtEmail.Text, txtPhone.Text, txtStreet1.Text, txtStreet2.Text, "", txtCity.Text, ddlState.SelectedValue, ddlCountry.SelectedValue, txtZipCode.Text);
        }

        Response.Redirect("default.aspx");
    }

}